new Vue({
    el: '#permissions',
    data: function () {
    	return {
    		uamsForm: {
    			uamsMenus: [{
	    			menuId: '',
	    			menuParentId: '',
	    			menuName: '',
	    			menuUrl: '',
	    			systemId: 'XBT',
	    			isParentMenu: ''
    			}],
    			uamsFunctions: [{
    				functionId: '',
	    			functionName: '',
	    			functionMenuId: ''
    			}],
    			uamsRoles: [{
    				role: '',
	    			roleFunctionId: ''
    			}]
    		},
    		xbtForm: {
    			xbtFormDatas: [{
    				menuClass: '',
    				menuIcon: '',
	    			menuParentId: '',
	    			menuName: '',
	    			menuType: '',
	    			menuUrl: '',
	    			menuSort: '',
	    			menuSystemId: 'XBT',
	    			menuCode: '',
	    			menuId: ''
    			}]
    		},
    		isParentMenus: ['是','否'],
    		roles: ['511001', '511002', '511003', '511004'],
    		roleFunctionIds:  [],//输入完功能id之后，把输入的的功能的id追加到这个数组中
    		menuTypes: ['MENU', 'BUTTON'],
            uamsInsertSqlResult:'',
            xbtInsertSqlResult:''
    	}
    },
    created: function () {
        String.prototype.startsWith = function (str){
            return this.slice(0, str.length) == str;
        };

        String.prototype.endsWith = function (str){
            return this.slice(-str.length) == str;
        };
    },
    methods: {
        /**
		 * 点击UAMS增加按钮
         */
        createUamsMenu: function () {
        	this.$refs.uamsResult.style.display = 'block';

        	var uamsMenusArr = this.uamsForm.uamsMenus;
        	var uamsFunctionsArr = this.uamsForm.uamsFunctions;
        	var uamsRolesArr = this.uamsForm.uamsRoles;

        	var menusStr = "";
            uamsMenusArr.forEach(function(e){
                if(!e.menuId){
                    alert("请输入菜单表的菜单id！");
                    return true;
                }
                if(!e.menuParentId){
                    alert("请输入菜单表的父菜单id！");
                    return true;
                }
                if(!e.menuName){
                    alert("请输入菜单表的菜单名称！");
                    return true;
                }
                if(!e.systemId){
                    alert("请输入菜单表的系统id！");
                    return true;
                }
            	if(e.isParentMenu=="是"){
					//父菜单
                    menusStr += "insert into DB2ADMIN.UMMENUS(tm_id,tm_pid,tm_name,tm_grade,tm_url,tm_sid,tm_order,tm_owner_dept_id,tm_is_has_charge) values('"+e.menuId+"','1','"+e.menuName+"',1,'','"+e.systemId+"',1,'10010041','N');	";
                }else{
            		//子菜单
                    menusStr += "insert into DB2ADMIN.UMMENUS(tm_id,tm_pid,tm_name,tm_grade,tm_url,tm_sid,tm_order,tm_owner_dept_id,tm_is_has_charge) values('"+e.menuId+"','"+e.menuParentId+"','"+e.menuName+"',2,'"+e.menuUrl+"','"+e.systemId+"',2,'10010041','N');	";
				}
			});
            uamsFunctionsArr.forEach(function(e){
                if(!e.functionId){
                    alert("请输入功能表的功能id！");
                    return true;
                }
                if(!e.functionMenuId){
                    alert("请输入功能表的菜单id！");
                    return true;
                }
                if(!e.functionName){
                    alert("请输入功能表的功能名称！");
                    return true;
                }
                menusStr += "insert into DB2ADMIN.UMFUNCT(TF_ID,TF_NAME,TF_URL,TF_MENUID,TF_CHARGE,TF_CREATOR,TF_BLDTIME,TF_MEMO_A,TF_MEMO_B,TF_OWNER_DEPT_ID,TF_IS_HAS_CHARGE,TF_USER_DEPT_ID) values('"+e.functionId+"','"+e.functionName+"','','"+e.functionMenuId+"','','sysadmin','2018-07-26','','','10010041','N','');	";
            });
            uamsRolesArr.forEach(function(e){
                if(!e.role){
                    alert("请选择角色！");
                    return true;
                }
                if(!e.roleFunctionId){
                    alert("请选择功能id！");
                    return true;
                }
                e.roleFunctionId.forEach(function (e1) {
                    menusStr += "insert into DB2ADMIN.UMRLAUT(TRA_ID,TRA_FUNCID) values('"+e.role+"','"+e1+"');	";
                })
            });
            this.uamsInsertSqlResult = menusStr;
        },
        /**
		 * 点击了UAMS修改按钮
         */
        updateUamsMenu: function () {
        	this.$refs.xbtResult.style.display = 'block';
			console.info("点击了UAMS的修改按钮");


            var uamsMenusArr = this.uamsForm.uamsMenus;
            var uamsFunctionsArr = this.uamsForm.uamsFunctions;
            var uamsRolesArr = this.uamsForm.uamsRoles;

            var menusStr = "";
            uamsMenusArr.forEach(function(e){
                if(!e.menuId){
                    alert("请选择菜单表菜单id！");
                    return true;
                }
                menusStr += "update DB2ADMIN.UMMENUS set";
                if(e.isParentMenu=="是"){
                    //父菜单
                    if(e.menuName){
                        menusStr +=" tm_name = '"+e.menuName+"',";
                    }
                    if(e.systemId){
                        menusStr +=" tm_sid = '"+e.systemId+"',";
					}
                }else{
                    //子菜单
                    if(e.menuParentId){
                        menusStr +=" tm_pid = '"+e.menuParentId+"',";
                    }
                    if(e.menuName){
                        menusStr +=" tm_name = '"+e.menuName+"',";
                    }
                    if(e.menuUrl){
                        menusStr +=" tm_url = '"+e.menuUrl+"',";
                    }
                    if(e.systemId){
                        menusStr +=" tm_sid = '"+e.systemId+"',";
                    }
                }
                if(menusStr.endsWith(",")){
                    menusStr = menusStr.substring(0,menusStr.length-1)
                }

                menusStr += " where tm_id = '"+e.menuId+"';";

            });
            uamsFunctionsArr.forEach(function(e){
                if(!e.functionId){
                    alert("请输入功能表的功能id！");
                    return true;
                }
                if(!e.functionName && !e.functionMenuId){
                    alert("请输入需要修改功能表的功能名或菜单id！");
                    return true;
                }
                menusStr += "update DB2ADMIN.UMFUNCT set";
                if(e.functionName){
                    menusStr += " TF_NAME = '"+e.functionName+"',";
				}
                if(e.functionMenuId){
                    menusStr += " TF_MENUID = '"+e.functionMenuId+"',";
                }

                if(menusStr.endsWith(",")){
                    menusStr = menusStr.substring(0,menusStr.length-1);
                }

                menusStr += " where TF_ID = '"+e.functionId+"';";
            });
            uamsRolesArr.forEach(function(e){
                if(!e.role){
                    alert("请选择角色！");
                    return true;
                }
                if(e.roleFunctionId.length == 0){
                    alert("请选择功能id！");
                    return true;
                }
                e.roleFunctionId.forEach(function (e1) {
                    menusStr += "insert into DB2ADMIN.UMRLAUT(TRA_ID,TRA_FUNCID) values('"+e.role+"','"+e1+"');	";
                })
            });
            this.uamsInsertSqlResult = menusStr;
        },
        /**
         * 点击信保通增加按钮
         */
        createXbtMenu: function () {
        	this.$refs.xbtResult.style.display = 'block';
            console.log("点击了信保通的增加按钮");
            var xbtSqlResult = "";
            var xbtFormDatasArr = this.xbtForm.xbtFormDatas;
            xbtFormDatasArr.forEach(function (e) {
                if(!e.menuClass && !e.menuIcon && !e.menuParentId && !e.menuName && !e.menuType && !e.menuUrl && !e.menuSort && !e.menuCode){
                    alert("请输入内容！");
                    return true;
                }
                if(!e.menuCode){
                    alert("请输入菜单编码！");
                    return;
                }
                if(!e.menuId){
                    alert("请输入该条数据的菜单ID！");
                    return;
                }

                if(e.menuType=="MENU"){//菜单
                	if(!e.menuIcon){
                		alert("类型为MENU时，菜单图标不能为空！");
						return true;
					}

                    if(e.menuClass){//父菜单
                        xbtSqlResult += "INSERT INTO `xbtdb`.`resource` (`ID`,`CLASSS`, `CREATENAME`, `CREATETIME`, `ICON`, `PARENTID`, `SOURCENAME`, `STATE`, `UPDATENAME`, `UPDATETIME`, `TYPE`, `URL`, `ORDER`, `SYSID`, `MENUCODE`) VALUES ('"+e.menuId+"','pcoded-hasmenu', 'admin', '2018-07-27 10:17:16', '"+e.menuIcon+"', '"+e.menuParentId+"', '"+e.menuName+"', '1', 'admin', '2018-07-27 10:17:48', 'MENU', '"+e.menuUrl+"', '"+e.menuSort+"', '"+e.menuSystemId+"', '"+e.menuCode+"');	";
                    }else {//子菜单
                        xbtSqlResult += "INSERT INTO `xbtdb`.`resource` (`ID`,`CLASSS`, `CREATENAME`, `CREATETIME`, `ICON`, `PARENTID`, `SOURCENAME`, `STATE`, `UPDATENAME`, `UPDATETIME`, `TYPE`, `URL`, `ORDER`, `SYSID`, `MENUCODE`) VALUES ('"+e.menuId+"',NULL, 'admin', '2018-07-27 10:18:13', '"+e.menuIcon+"', '"+e.menuParentId+"', '"+e.menuName+"', '1', 'admin', '2018-07-27 10:18:43', 'MENU', '"+e.menuUrl+"', '"+e.menuSort+"', '"+e.menuSystemId+"', '"+e.menuCode+"');	";
                    }
                }else{//功能
                    xbtSqlResult += "INSERT INTO `xbtdb`.`resource` (`ID`,`CLASSS`, `CREATENAME`, `CREATETIME`, `ICON`, `PARENTID`, `SOURCENAME`, `STATE`, `UPDATENAME`, `UPDATETIME`, `TYPE`, `URL`, `ORDER`, `SYSID`, `MENUCODE`) VALUES ('"+e.menuId+"',NULL, 'admin', '2018-07-27 10:18:13', '"+e.menuIcon+"', '"+e.menuParentId+"', '"+e.menuName+"', '1', 'admin', '2018-07-27 10:18:43', 'BUTTON', '"+e.menuUrl+"', '"+e.menuSort+"', '"+e.menuSystemId+"', '"+e.menuCode+"');	";
                }
            })
			this.xbtInsertSqlResult=xbtSqlResult;
        },
        /**
         * 点击了信保通修改按钮
         */
        updateXbtMenu: function () {
        	this.$refs.xbtResult.style.display = 'block';
            console.info("点击了信保通的修改按钮");

			var xtbSqlResult = "";
            var xbtFormDatasArr = this.xbtForm.xbtFormDatas;
            xbtFormDatasArr.forEach(function (e) {
            	if(!e.menuClass && !e.menuIcon && !e.menuParentId && !e.menuName && !e.menuType && !e.menuUrl && !e.menuSort && !e.menuCode && !e.menuId){
                    alert("请输入内容！");
            		return true;
				}
                if(!e.menuId){
                    alert("请输入该条数据的菜单ID，用作修改条件");
                    return;
                }

                xtbSqlResult += "UPDATE `xbtdb`.`resource` SET ";

            	if(e.menuClass){
                    xtbSqlResult += "`CLASSS`='"+e.menuClass+"',";
				}
                if(e.menuIcon){
                    xtbSqlResult += "`ICON`='"+e.menuIcon+"',";
                }
                if(e.menuParentId){
                    xtbSqlResult += "`PARENTID`='"+e.menuParentId+"',";
                }
                if(e.menuName){
                    xtbSqlResult += "`SOURCENAME`='"+e.menuName+"',";
                }
                if(e.menuType){
                    xtbSqlResult += "`TYPE`='"+e.menuType+"',";
                }
                if(e.menuUrl){
                    xtbSqlResult += "`URL`='"+e.menuUrl+"',";
                }
                if(e.menuSort){
                    xtbSqlResult += "`ORDER`='"+e.menuSort+"',";
                }
                if(e.menuSystemId){
                    xtbSqlResult += "`SYSID`='"+e.menuSystemId+"',";
                }
                if(e.menuCode){
                    xtbSqlResult += "`MENUCODE`='"+e.menuCode+"',";
                }

                if(xtbSqlResult.endsWith(",")){
                    xtbSqlResult = xtbSqlResult.substring(0,xtbSqlResult.length-1);
                }

                xtbSqlResult += " WHERE `ID`='"+e.menuId+"'; ";
			})

            this.xbtInsertSqlResult=xtbSqlResult;
        },
        /**
         * 增加uams列表
         */
        addUamsItem: function (name) {
			this.uamsForm[name].push({
				menuId: '',
    			menuParentId: '',
    			menuName: '',
    			menuUrl: '',
    			systemId: 'XBT',
    			isParentMenu: '',
    			functionId: '',
    			functionName: '',
    			functionMenuId: '',
    			role: '',
    			roleFunctionId: ''
			})
		},
		/**
         * 删除uams列表
         */
		removeUamsItem: function (item, name) {
			var index = this.uamsForm[name].indexOf(item);
			if (index !== -1) {
				this.uamsForm[name].splice(index, 1);
			}
            if(name == "uamsFunctions"){
                this.getFunctionId();
            }
		},
        /**
         * 增加xbt列表
         */
        addXbtItem: function (name) {
			this.xbtForm[name].push({
                menuClass: '',
				menuIcon: '',
    			menuParentId: '',
    			menuName: '',
    			menuType: '',
    			menuUrl: '',
    			menuSort: '',
    			menuSystemId: 'XBT',
    			menuCode: '',
                menuId: ''
			})
		},
		/**
         * 删除xbt列表
         */
		removeXbtItem: function (item, name) {
			var index = this.xbtForm[name].indexOf(item);
			if (index !== -1) {
				this.xbtForm[name].splice(index, 1);
			}
		},
        /**
		 * 功能id输入框失去焦点事件
		 * 获取到所有的功能id放到roleFunctionIds数组中
         */
        getFunctionId:function () {
            var _this = this;
            var uamsFunctionsArr = _this.uamsForm.uamsFunctions;
            _this.roleFunctionIds = [];
            uamsFunctionsArr.forEach(function (e) {
                _this.roleFunctionIds.push(e.functionId);
			})
        }
    }
  })